//
//  MoAlertController.swift
//  dycommon_Example
//
//  Created by zhuxietong on 2021/1/31.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import Foundation
import UIKit
import Eelay

extension UIView{
    public var __modalView:MoAlertController.ModalView?{
        if let v = self as? MoAlertController.ModalView{
            return v
        }
        return self.superview?.__modalView
    }
    public var __keyboarAvoidBack:MoAlertController.KeyboadAvoidBack?{
        if let v = self as? MoAlertController.KeyboadAvoidBack{
            return v
        }
        return self.superview?.__keyboarAvoidBack
    }
    
}
extension MoAlertController{
    

    public func responderView(_ view: UIView) -> UIView? {
        for childView in view.subviews {
            if childView.responds(to: #selector(getter: isFirstResponder)) && childView.isFirstResponder {
                return childView
            }
            let result = responderView(childView)
            if result != nil {
                return result
            }
        }
        return nil
    }
    
    

 
    
    
    public func initV() {
//        NotificationCenter.default.addObserver(self, selector: #selector(focus), name: UITextView.textDidBeginEditingNotification, object: nil)
//        NotificationCenter.default.addObserver(self, selector: #selector(focus), name: UITextField.textDidBeginEditingNotification, object: nil)
        
        keyboard.filterSameFrame = false
        keyboard.change = {[weak self]
                status in
                print("0000-llll-------s",status)
                guard let ws = self else {
                    return
                }
                switch status {
                case .changeFrame(let f, _):
                    self?.keyboadFrame = f
                    ws.focus()
                    break
                case .didHide:
                    break
                case .didShow(let f, _):
                    self?.keyboadFrame = f
//                    ws.focus()
                    break
                case .willHide:
                    ws.lostFocus()
                case .willShow(let f, _):
                    self?.keyboadFrame = f
                    ws.focus()
                    break
                }
            }

    }
    @objc func focus(){
        if keyboadFrame.size.height < 10{
            return
        }
        if appearStatus != .didAppear{return}
        let field = responderView(view)
        let back = field?.__keyboarAvoidBack
        
        guard let f = field,let superV = f.superview,let window = UIApplication.shared.keyWindow, let b = back else {
            return
        }
        
//        let window = self.view
        //                let toSuperFrame = superView.convert(view.frame, to: superView.superview!)
        let toWindowFrame = superV.convert(f.frame, to: window)

        let bottomDistance = Sheight - (toWindowFrame.origin.y + toWindowFrame.size.height) - keyBoardAvoidJust
//        print("toWindowFrame-----",bottomDistance,keyboadFrame.size.height)

        var moveUp:CGFloat = 0
        if bottomDistance < keyboadFrame.size.height{
            
            
            var just:CGFloat = keyBoardAvoidJust
            if (topConstrain.constant > 10){
                just = 0
            }
            
            moveUp = keyboadFrame.size.height - bottomDistance + (-topConstrain.constant)
//            print("bottomDistance-----",bottomDistance,keyboadFrame.size.height)
        }
        
        if moveUp > 0{
            topConstrain.constant = -moveUp
            view.setNeedsLayout()
            view.layoutIfNeeded()
            keyboadAvoidBack.setNeedsLayout()
            keyboadAvoidBack.layoutIfNeeded()
        }
        
//        print("bottomDistance-----",bottomDistance)

            

        let justDistance = keyboadFrame
//                let windowF = ctr.view.convert(ctr.postionBar.frame, to: window)
//                let justY = windowF.origin.y - toWindowFrame.origin.y
////                print("+++++",justY)
//                let just = jo_contentView.bounds.height - 56 + justY
//                bar_back.frame = CGRect(x: 0, y: just, width: w, height: 56)
//                print(")))S-",toWindowFrame,windowF)
            
        
    }
    
    @objc func lostFocus(){
//        let r = responderView(view)
//        let modal = r?.__modalView
//
        topConstrain.constant = 0
        view.setNeedsLayout()
        view.layoutIfNeeded()
        keyboadAvoidBack.setNeedsLayout()
        keyboadAvoidBack.layoutIfNeeded()
        keyboadFrame = .zero
    }

    

}

